import { actBusinessEdit, actBusinessAdd, actBusinessGet } from '@/api/actBusiness'
import Vue from 'vue'
import JEllipsis from '@/components/jeecg/JEllipsis'
import { getDictItemsValue } from '@api/api'
import { getFileAccessHttpUrl } from '@api/manage'
import {  getRefPromise } from '@/utils/JEditableTableUtil'

export const ActivitiFormMixin = {

  props: {
    /*全局禁用，可表示查看*/
    disabled: {
      type: Boolean,
      default: false,
      required: false
    },
    /*流程数据*/
    processData: {
      type: Object,
      default: () => {
        return {}
      },
      required: false
    },
    /*是否新增*/
    isNew: { type: Boolean, default: false, required: false },
    /*是否处理流程*/
    task: { type: Boolean, default: false, required: false }
  },
  data () {
    return {
      description: '流程表单demo，按例开发表单。需在 activitiMixin.js 中加入写好的表单',
      // form
      /*表单回显数据*/
      data: {},
      oldDataMap: {},
      btndisabled: false,
      childLoading: false,
      confirmLoading: false,
      /**表单回显数据*/
      dictTextData: {}
    }
  },
  methods: {
    /*回显数据*/
    init () {
      let that = this
      this.btndisabled = true
      let r = this.processData
      if (r.tableId) {
        this.confirmLoading = true
        let type = 1
        // if (r.disabled) {
        //   type = 0
        // }
        if (r.executionId || r.businessId) {
          type = 0
        }
        actBusinessGet({
          type: type,
          tableId: r.tableId,
          tableName: r.tableName,
          executionId: r.executionId,
          businessId: r.businessId
        }).then((res) => {
          if (res.success) {
            console.log(res.result)
            let formData = res.result
            formData.dataMap.versionFlag = ''
            that.data = formData
            that.btndisabled = false
            if (type == 1){
              that.oldDataMap = formData.dataMap
            } else {
              that.oldDataMap = formData.oldDataMap
            }
            // that.setValue()
            if (typeof that.setValue === 'function') that.setValue(this.model)
          } else {
            that.$message.error(res.message)
          }
        }).finally(() => {
          that.confirmLoading = false
        })
      }
    },
    // setValue(){
    //
    // },
    // handler
    /** ATab 选项卡切换事件 */
    handleChangeTabs(key) {
      // 自动重置scrollTop状态，防止出现白屏
      getRefPromise(this, key).then(editableTable => {
        editableTable.resetScrollTop()
      })
    },
    handleSubmitVa (values) {
      let formData = values
      formData.tableId = this.processData.tableId
      let action
      if (!this.isNew) {
        formData.id = this.processData.id
        action = actBusinessEdit(formData)
      } else {
        action = actBusinessAdd(formData)
      }
      this.btndisabled = true
      action.then((res) => {
        if (res.success) {
          this.$message.success('保存成功！')
          this.$emit('afterSubmit', formData)
        } else {
          this.$message.error(res.message)
        }
      }).finally(() => {
        this.btndisabled = false
      })
    },
    close () {
      //todo 关闭后的回调
      this.$emit('close')
    },
    /*通过审批*/
    passTask () {
      this.$emit('passTask')
    },
    /*驳回审批*/
    backTask () {
      this.$emit('backTask')
    },
    /**
     * 查询字典信息
     * @param val 字典的值
     * @param code 字典CODE
     * @param te 返回值显示的字段
     */
    dictText (val, code, te) {
      let that = this
      that.data[te] = ''
      if (val && code) {
        getDictItemsValue({dictCode: code, key: val}).then((res) => {
          if (res.success) {
            // if (typeof fun === 'function') fun(res.result)
            that.data[te] = res.result
            that.$forceUpdate()
          }
        })
      }
    },
    /* 图片预览 */
    getImgView(text){
      if(text && text.indexOf(",")>0){
        text = text.substring(0,text.indexOf(","))
      }
      return getFileAccessHttpUrl(text)
    },
    /* 文件下载 */
    uploadFile(text){
      if(!text){
        this.$message.warning("未知的文件")
        return;
      }
      if(text.indexOf(",")>0){
        text = text.substring(0,text.indexOf(","))
      }
      let url = getFileAccessHttpUrl(text)
      window.open(url);
    },

    /**
     *
     * @param url 请求的url
     * @param params 请求的参数
     * @param tab 返回的赋值字段
     */
    getListByUrl(url, params, tab, success) {

      tab.loading = true;
      this.getAction(url, params).then(res => {

        let { result } = res
        let dataSource = []
        if (result) {
          if (Array.isArray(result)) {
            dataSource = result
          } else if (Array.isArray(result.records)) {
            dataSource = result.records
          }
        }
        tab.dataSource = dataSource
        typeof success === 'function' ? success(res) : ''

        //
        // this.childLoading = false;
        // if (res.success) {
        //   blackDate = res.result||[];
        // }else {
        //   // this.$message.error(res.message)
        // }
      }).finally(() => {
        tab.loading = false;
      });
    },
  }
}